package com.yunbiao.ybbusstopplate.ui.view

import android.content.Context
import android.graphics.Color
import android.media.MediaPlayer
import android.os.Handler
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.VideoView
import com.blankj.utilcode.util.UiMessageUtils
import com.yunbiao.ybbusstopplate.base.Constants
import com.yunbiao.ybbusstopplate.bean.ResouceBean
import com.yunbiao.ybbusstopplate.doublescreen.AdvertView
import com.yunbiao.ybbusstopplate.utils.BitmapRecycle
import com.yunbiao.ybbusstopplate.utils.FileTool
import com.yunbiao.ybbusstopplate.utils.GlideLoadUtils

class AutoPlayViewBottom : FrameLayout{
    val TAG = "AutoPlayViewBottom"
    private var imageView:ImageView?=null
    private var videoView:VideoView?=null
    private var linearLayout:LinearLayout?=null

    private var resourceList:List<ResouceBean>?=null
    private var uiHandler:Handler?=null
    private var playRunnable:PlayRunnable?=null
    private var isPlay:Boolean=true
    private var playTime:Int=15
    private var bitmapRecycle:BitmapRecycle?=null

    constructor(context: Context):super(context){
        initView()
    }

    private fun initView(){
        bitmapRecycle= BitmapRecycle()

        imageView=ImageView(context)
        imageView!!.scaleType=ImageView.ScaleType.FIT_XY

        videoView= VideoView(context)

        linearLayout= LinearLayout(context)
        linearLayout!!.setBackgroundColor(Color.parseColor("#ffffff"))

        var layoutParams=LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT)
        layoutParams.gravity=Gravity.CENTER

        var videoParams=LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)
        videoParams.gravity=Gravity.CENTER

        imageView?.visibility= View.GONE
        videoView?.visibility=View.GONE

        linearLayout!!.addView(videoView,videoParams)
        linearLayout!!.addView(imageView,layoutParams)

        initListener()

        uiHandler=Handler(context.mainLooper)
        playRunnable=PlayRunnable()

        addView(linearLayout,layoutParams)
    }

    private fun initListener(){
        //加载成功
        videoView!!.setOnPreparedListener(object :MediaPlayer.OnPreparedListener{
            override fun onPrepared(p0: MediaPlayer?) {
                p0!!.start()
            }
        })
        //播放完成
        videoView!!.setOnCompletionListener(object :MediaPlayer.OnCompletionListener{
            override fun onCompletion(p0: MediaPlayer?) {
                if (resourceList!!.size>=1){
                    uiHandler!!.postDelayed(playRunnable!!,100)
                }else{
                    p0!!.start()
                }
            }
        })
        //播放出错
        videoView!!.setOnErrorListener(object :MediaPlayer.OnErrorListener{
            override fun onError(p0: MediaPlayer?, p1: Int, p2: Int): Boolean {
                if (resourceList!!.size>=1){
                    uiHandler!!.postDelayed(playRunnable!!,100)
                }
                return true
            }
        })
    }

    fun start(resourceList:List<ResouceBean>){
        Log.d(TAG, "底部播放 " )
        this.resourceList=resourceList
        if (resourceList==null||resourceList.isEmpty()){
            return
        }
        isPlay=true
        uiHandler!!.post(playRunnable!!)
    }

    fun stop(){
        isPlay=false
        if (uiHandler!=null&&playRunnable!=null){
            uiHandler!!.removeCallbacks(playRunnable!!)
        }
    }

    fun destry(){
        UiMessageUtils.getInstance().send(AdvertView.DESTROY);
        isPlay=false
        if (uiHandler!=null&&playRunnable!=null){
            uiHandler!!.removeCallbacks(playRunnable!!)
        }
        if (videoView!=null){
            videoView!!.destroyDrawingCache()
            videoView!!.visibility= View.GONE
        }
    }
    var count:Int=0
    //匿名内部类
    inner class PlayRunnable:Runnable{
        override fun run() {
            if (!isPlay){
                return
            }
            // 这里如果是三分屏，临时使用中部的第一个文件作为底部播放文件，后面可改
            count=0
//            if (count>=resourceList!!.size){
//                count=0
//            }
            val resource = resourceList!!.get(count)
            if (resource.type==FileTool.FileType.VIDEO){
                playVideo(resource.path)
            }else{

                var resourcePath = resource.path

                Log.d("net", "暂时 默认 中部广告: " + resourcePath)
//                // 中部广告 资源位置
//                resourcePath = Constants.advertCenterResourcePath

                playImage(resourcePath)
                Log.e(TAG, "run: ----------resource.path---------------->"+resource.path )

                if (resourceList!!.size>1){
                    if (resource.playTime!=0){
                        uiHandler!!.postDelayed(playRunnable!!,resource.playTime*1000L)
                    }else{
                        uiHandler!!.postDelayed(playRunnable!!,playTime*1000L)
                    }

                }
            }
            count++
        }
    }

    private fun playImage(path:String){
        GlideLoadUtils.getInstance().loadLocalImage(context,path,imageView,linearLayout,"0",bitmapRecycle)
        videoView!!.visibility= View.GONE
        imageView!!.visibility=View.VISIBLE
        UiMessageUtils.getInstance().send(AdvertView.IMAGE,path)
    }

    private fun playVideo(path:String){
        videoView!!.setVideoPath(path)
        imageView!!.visibility= View.GONE
        linearLayout!!.setBackgroundColor(Color.parseColor("#000000"))
        videoView!!.visibility=View.VISIBLE
        videoView!!.setZOrderOnTop(true)
        UiMessageUtils.getInstance().send(AdvertView.VIDEO,path)
    }

    override fun onDetachedFromWindow() {
        destry()
        super.onDetachedFromWindow()
    }
}